package com.zxh.dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.WebServlet;

import com.zxh.entity.Education;
import com.zxh.utils.DBUtils;
import com.zxh.utils.DateUtils;
/**
 * 保存教育经历
 * @author liyunf
 *
 */

public class EducationDao {
	public void save(Education education){
		String sql = "insert into tb_education(applicant_id,school,beginTime,schooling,profession) values(?,?,?,?,?)";
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBUtils.getConnection(); // 获取数据库的连接
			ps = con.prepareStatement(sql);//执行预编译的 sql语句
			ps.setInt(1, education.getApplicant_id());
			ps.setString(2,education.getSchool() );
			ps.setTimestamp(3, education.getBeginTime()==null?null : new Timestamp(DateUtils.getStringToDate(education.getBeginTime()).getTime()));
			ps.setString(4, education.getSchooling());
			ps.setString(5, education.getProfession());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, null, null, ps);
		}
	}
	public List<Education> getEducationList(int applicant_id){
		String sql = "select * from tb_education where applicant_id=?";
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<Education> list = new ArrayList<Education>();
		try {
			con = DBUtils.getConnection(); // 获取数据库的连接
			ps = con.prepareStatement(sql);//执行预编译的 sql语句
			ps.setInt(1, applicant_id);
			rs = ps.executeQuery();
			Education education = null;
			while(rs.next()){
				education = new Education();
				education.setEducation_id(rs.getInt("education_id"));
				education.setApplicant_id(rs.getInt("applicant_id"));
				education.setSchool(rs.getString("school"));
				education.setBeginTime(DateUtils.getDateToString(rs.getDate("beginTime")));
				education.setSchooling(rs.getString("schooling"));
				education.setProfession(rs.getString("profession"));
				list.add(education);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, rs, null, ps);
		}
		return list;
	}
	/**
	 * 修改教育经历
	 */
	
	public void update(Education education){
		String sql = "update tb_education set school=?,beginTime=?,schooling=?,profession=?  where education_id=? ";
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBUtils.getConnection(); // 获取数据库的连接
			ps = con.prepareStatement(sql);//执行预编译的 sql语句
			ps.setString(1,education.getSchool() );
			ps.setTimestamp(2, education.getBeginTime()==null?null : new Timestamp(DateUtils.getStringToDate(education.getBeginTime()).getTime()));
			ps.setString(3, education.getSchooling());
			ps.setString(4, education.getProfession());
			ps.setInt(5, education.getEducation_id());
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, null, null, ps);
		}
	}
	
	/**
	 * 删除教育经历
	 */
	public void delete(int education_id){
		String sql="delete from tb_education where education_id=? ";
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = DBUtils.getConnection(); // 获取数据库的连接
			ps = con.prepareStatement(sql);//执行预编译的 sql语句
			ps.setInt(1, education_id);
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(con, null, null, ps);
		}
	}
	}

